package com.fiestas.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.fiestas.db.DBManager;
import com.fiestas.R;

public class MainActivity extends Activity {

	
private EditText editTextUsername,editTextPassword;
private Button loginButton, registerButton;
private DBManager dbManager;

	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editTextUsername = (EditText) findViewById(R.id.editTextUserName);
        editTextPassword = (EditText) findViewById(R.id.editTextUserPassword);
        loginButton = (Button) findViewById(R.id.buttonLogin);
        registerButton = (Button) findViewById(R.id.buttonRegister);
        
        dbManager = new DBManager(this);
        loginButton.setOnClickListener(loginButtonListener());
        registerButton.setOnClickListener(registerButtonListener());
        
        
        
        
    }


	private OnClickListener registerButtonListener() {
		return new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				String name = editTextUsername.getText().toString();
				Integer password = Integer.parseInt(editTextPassword.getText().toString());
				dbManager.insertSocio(name, null, null,password);
			}
		};
	}


	private OnClickListener loginButtonListener() {
		return new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				String name = editTextUsername.getText().toString();
				String password = editTextPassword.getText().toString();
				String checkPassword = dbManager.queryPassword(name); 
				if(checkPassword.equals(password)){
					Intent intent = new Intent(MainActivity.this,ActivityUserProfile.class);
					intent.putExtra("name", name);
					startActivity(intent);
					System.out.println("CORRECTO");
				}else{
					System.out.println("INCORRECTO");
					System.out.println(checkPassword);
				}
				
				
			}
		};
	}


    
    
}
